//
//  ODContactViewController.swift
//  OdmIM
//
//  Created by Gao on 17/1/18.
//  Copyright © 2017年 OdmIM. All rights reserved.
//
// http://www.crifan.com/swift_implement_contact_list/

import UIKit

var gContactTableViewData = ODContactTableViewData()

class ODContactViewController: UIViewController {

    var contactTableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //update frame
        let viewFrameX = view.frame.origin.x
        let viewFrameY = view.frame.origin.y
        let viewFrameW = UIScreen.main.bounds.width
        var viewFrameH = UIScreen.main.bounds.height
        
        if prefersStatusBarHidden == false {
            viewFrameH -= 20
        }
        if navigationController?.prefersStatusBarHidden == false {
            viewFrameH -= 44
        }
        if tabBarController?.tabBar.isHidden == false {
            viewFrameH -= 49
        }
        let viewFrame = CGRect(x: viewFrameX, y: viewFrameY, width: viewFrameW, height: viewFrameH)
        
        view.frame = viewFrame
        
        //set to UITableViewStyle.Plain for support sectionIndexTitlesForTableView
        contactTableView = UITableView(frame: view.bounds, style: .plain)
        
        gContactTableViewData.updateContactData()
        
        contactTableView.delegate = self
        contactTableView.dataSource = self
//
//        contactTableView.backgroundColor = ColorContactTableViewBackground
//        contactTableView.separatorColor = ColorContactTableViewCellSeperator
        contactTableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 0)
        contactTableView.separatorStyle = .singleLine
        
        //improve performance
//        contactTableView.estimatedRowHeight = HeightContactTableViewCell
//        
//        contactTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: StrIdContactTableViewCell)
        view.addSubview(self.contactTableView)

    }
}
/***************************************************************************
 * UITableViewDataSource functions
 ***************************************************************************/
extension ODContactViewController: UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return gContactTableViewData.contactSectionItemList.count
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return gContactTableViewData.contactSectionItemList[section].contactItemList.count
    }
    
    //    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    //        //print("heightForRowAtIndexPath indexPath = \(indexPath)")
    //        return HeightContactTableViewCell
    //    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("cellForRowAtIndexPath indexPath = \(indexPath)")
        
        let cell = UITableViewCell(style: .default, reuseIdentifier: "ContactTableViewCell")
        cell.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        
        cell.selectionStyle = .blue
        cell.accessoryType = .none
        cell.accessoryView = nil
        
        cell.detailTextLabel?.text = nil
        
        //                cell.textLabel?.font = FontContactTableViewCellText
        cell.textLabel?.textAlignment = .left
        //                cell.textLabel?.textColor = ColorContactTableViewCellText
        
        let curContactSectionItem = gContactTableViewData.contactSectionItemList[indexPath.section]
        let curContactItemInSection = curContactSectionItem.contactItemList[indexPath.row]
        cell.textLabel?.text = curContactItemInSection.nameStr
        
        //                let cornerHeaderImage:UIImage = drawSingleCornerHeaderImage(getFirstChar((cell.textLabel?.text)!), headerLabelFont: FontContactTableViewCellText, headerImageSize: SizeContactTableViewCellImage)
        //
        //                cell.imageView?.image = cornerHeaderImage
        
        return cell
    }
    
    //right side: "A" ... "Z"
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {

        return gContactTableViewData.contactIndexTitleList
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

        return gContactTableViewData.contactSectionItemList[section].indexTitleStr
    }
    
}

/***************************************************************************
 * UITableViewDelegate functions
 ***************************************************************************/
extension ODContactViewController: UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        self.performSegue(withIdentifier: "tapOnCell", sender: self)
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        
        self.tabBarController?.tabBar.isHidden = false
        
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        
        //userid:guyanqiu
        //name:guyanqiu
        //portraitUri:http://img3.imgtn.bdimg.com/it/u=214931719,1608091472&fm=21&gp=0.jpg
        //{"code":200,"userId":"guyanqiu","token":"IeODWt94pGEOTb+l1O+9aFJsM8sWxg1LHIvDjv5UFRbUbUT7wuqlDFpdCGHvi7pC3Cps8FxIvzb9Lip4D33GP9CQeXZh3A9t"}
        
        
        let destVC = segue.destination as? RCConversationViewController
        
//        destVC?.targetId = "guyanqiu"
//        destVC?.conversationType = RCConversationType.ConversationType_PRIVATE
//        destVC?.title = "guyanqiu"
        
        destVC?.targetId = "gaoh"
        destVC?.conversationType = RCConversationType.ConversationType_PRIVATE
        destVC?.title = "gaoh"
        
        self.tabBarController?.tabBar.isHidden = true
        
    }
    
    
}

